<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="openwindow(1,'0')">添加权限</el-button>
        </div>
        <el-table
          border
          :data="list"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          row-key="id"
        >
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button v-if="row.type === 1" size="small" type="text" @click="openwindow(2, row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="putshuju(row)">查看</el-button>
              <el-button size="small" type="text" @click="remove(row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>

    </div>
    <!-- 新增编辑的弹层 -->
    <el-dialog :visible="showDialog" :title="title" @close="shutdown">
      <!-- 表单内容 -->
      <el-form ref="formdia" label-width="100px" :model="formData" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <div style="text-align: right;">
          <el-button @click="shutdown">取消</el-button>
          <el-button type="primary" @click="addshuju">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getpermissin, delPermissionApi, addPermissionApi, getPermissionDetailApi, updatePermissionApi } from '@/api/permission'
import { getTree } from '@/utils/index'
export default {
  name: 'Permission',
  data() {
    return {
      // 存放渲染的数据
      list: [],
      // 控制弹窗显示
      showDialog: false,
      // 弹窗的绑定数据
      formData: {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      },
      // 规则
      rules: {
        name: [{ required: true, message: '请输入权限名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入权限标识', trigger: 'blur' }],
        description: [{ required: true, message: '请输入权限描述', trigger: 'blur' }]
      },
      title: ''

    }
  },
  created() {
    this.getpermission()
  },
  methods: {
    // 获取初始渲染数据
    async getpermission() {
      const { data } = await getpermissin()
      // console.log(data)
      this.list = getTree(data, '0')
      // console.log(this.list)
    },
    // 点击删除的时候
    remove(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delPermissionApi(row.id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getpermission()
      }).catch(() => { })
    },
    // 点击添加打开弹窗
    openwindow(type, pid) {
      this.title = '添加数据'
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    // 关闭弹窗
    shutdown() {
      this.$refs.formdia.resetFields()
      this.showDialog = false
      this.formData = {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      }
    },
    // 点击确定的时候校验数据并添加
    addshuju() {
      this.$refs.formdia.validate(async value => {
        if (!value) return
        if (this.formData.id) {
          const res = await updatePermissionApi(this.formData)
          console.log(res)
          this.$message.success('修改成功')
        } else {
          const res = await addPermissionApi(this.formData)
          console.log(res)
          this.$message.success('添加成功')
        }

        this.getpermission()
        this.shutdown()
      }
      )
    },
    // 点击查看请求接口并且获取数据
    async putshuju(row) {
      const { data } = await getPermissionDetailApi(row.id)
      // console.log(res)
      this.formData = data
      this.title = '修改数据'
      this.showDialog = true
    }
  }

}
</script>

<style>

</style>
